package binaryOctopus;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Time;
import java.util.Scanner;

/**
 * The Stats is used to keep track of statistics and to manage the 
 * statistics file. Therefore after each game it will overwrite 
 * any values that have been changed.
 */
public class Stats 
{
	//stats in the statistic file
	private int wins, loses, draws = 0;
	//time that will be used to record fastest time (enter in milliseconds, which should
	//be how it is captured)
	private Time time = Time.valueOf("00:00:00");
	//the statistics file
	private String fileName;
	//the statistics file reader
	private Scanner input;
	//the statistics file writer
	private BufferedWriter output;
	
	
	/**
	 * Constructor that will read in a Statistics file and will parse it to
	 * be able to read wins, loses, draws and the fastest time
	 * @param input name of Statistics file that will be located within file directory
	 */
	public Stats(String fileName)
	{
		this.fileName = fileName;
		File file = new File(fileName);
		boolean newlyMade = false;
		if(!file.exists())
		{
			try 
			{
				file.createNewFile();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			newlyMade = true;
		}	
		if(!newlyMade)
		{
			parseFile();
		}
		else
		{
			reWriteStats();
		}
	}
	
	/**
	 * parses file to read wins, loses, draws and fastest time
	 */
	private void parseFile()
	{
		try
		{
			this.input = new Scanner(new FileInputStream(fileName));
			this.wins = Integer.parseInt(input.nextLine());
			this.loses = Integer.parseInt(input.nextLine());
			this.draws = Integer.parseInt(input.nextLine());
			this.time = Time.valueOf(input.nextLine());
			input.close();
		}
		catch (Exception e)
		{
			System.out.println("Error parsing file");
		}
	}
	
	/**
	 * This will rewrite the statistics file so that it updates 
	 * the statistics after each instance of a game.
	 */
	public void reWriteStats()
	{
		try 
		{
			this.output = new BufferedWriter(new FileWriter(fileName));
			output.write(wins + "\n" + loses + "\n" + draws + "\n" + time);
			this.output.close();
		} 
		catch (IOException e) 
    {
			System.out.println("Error writing file");
		}
	}
	
	/**
	 * Increment wins
	 */
	public void incrementWins()
	{
		wins++;
		reWriteStats();
	}
	
	/**
	 * Increment loses
	 */
	public void incrementLoses()
	{
		loses++;
		reWriteStats();
	}
	
	/**
	 * Increment draws
	 */
	public void incrementDraws()
	{
		draws++;
		reWriteStats();
	}
	
	/**
	 * Set a new fastest time
	 * @param time The time to set
	 */
	public void setFastestTime(Time time)
	{
		this.time = time;
		reWriteStats();
	}
	
	/**
	 * Returns the number of wins
	 * @return The number of wins
	 */
	public int getWins()
	{
		return wins;
	}
	
	/**
	 * Returns the number of loses
	 * @return The number of loses
	 */
	public int getLoses()
	{
		return loses;
	}
	
	/**
	 * Returns the number of draws
	 * @return The number of draws
	 */
	public int getDraws()
	{
		return draws;
	}
	
	/**
	 * Returns the current fastest time
	 * @return The current fastest time
	 */
	public Time getTime()
	{
		return time;
	}
	
	/**
	 * Resets the stats to initial values
	 */
	public void clearStats()
	{
		wins = 0;
		loses = 0;
		draws = 0;
		time = new Time(0);
		reWriteStats();
	}
}
